from sklearn import svm
from sklearn.cross_validation import cross_val_score
from sklearn.ensemble import RandomForestClassifier
from sklearn.ensemble import ExtraTreesClassifier
from sklearn.ensemble import GradientBoostingClassifier
from sklearn.decomposition import KernelPCA
import numpy as np
import pylab as pl

def main():
    #read in  data, parse into training and target sets
    dataset = np.genfromtxt(open('data/output.csv','r'),     \
                            delimiter=',', dtype='f8')[1:]
    target  = np.array([x[0] for x in dataset])
    train   = np.array([x[2:] for x in dataset])

    cfr = svm.SVC(kernel='rbf', gamma=0.0001, probability=True)
    scores = cross_val_score(cfr, train, target)
    print 'SVM(rbf): ', scores.mean()

    cfr = RandomForestClassifier(n_estimators=100, max_depth=None)
    scores = cross_val_score(cfr, train, target)
    print 'RandomForest: ', scores.mean()

    cfr = ExtraTreesClassifier(n_estimators=100, max_depth=None)
    scores = cross_val_score(cfr, train, target)
    print 'ExtraTrees: ', scores.mean()

    cfr = GradientBoostingClassifier(n_estimators=100, learn_rate=0.3)
    scores = cross_val_score(cfr, train, target)
    print 'GradientBoost: ', scores.mean()

    # disable, take too long
    #cfr = svm.SVC(kernel='linear', probability=True)
    #scores = cross_val_score(cfr, train, target)
    #print 'SVM(linear): ', scores.mean()

    pca = KernelPCA(n_components=2, kernel='linear')
    train = pca.fit_transform(train)

    reduced = train
    pl.figure(figsize=(8, 8))
    for idx in range(0, len(target)):
        if target[idx] == 1:
            pl.scatter(reduced[idx, 0], reduced[idx, 1], marker='x')
        else:
            pl.scatter(reduced[idx, 0], reduced[idx, 1], marker='o')
    pl.show()

if __name__=="__main__":
    main()
